//
//  ViewController.swift
//  day01
//
//  Created by 杨绍鑫 on 2018/9/4.
//  Copyright © 2018年 crcnet. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    let resetButton : UIButton = {
        let button = UIButton.init()
        button .setTitle("Reset", for: UIControlState.normal)
        return button
    }()
    
    let startButton : UIButton = {
        let startButton = UIButton.init()
        startButton.backgroundColor = UIColor.blue
        startButton.setImage(UIImage.init(named: "play"), for: .normal)
        return startButton
    }()
    
    let stopButton : UIButton = {
        let stopButton = UIButton.init()
        stopButton.backgroundColor = UIColor.green
        stopButton.setImage(UIImage.init(named: "pause"), for: .normal)
        return stopButton
    }()
    
    
    let showLable : UILabel = {
        let lable1 = UILabel.init()
        lable1.font = UIFont.systemFont(ofSize: 60)
        lable1.textAlignment = NSTextAlignment.center
        lable1.text = "0.0"
        lable1.textColor = UIColor.white
        return lable1
    }()
    
    var timer = Timer()
    var isPaying = false
    
    
    var counter = 0.0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
        self.setAction()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func setupUI() {
        
        self.view.addSubview(resetButton)
        self.view.addSubview(showLable)
        self.view.addSubview(startButton)
        self.view.addSubview(stopButton)
        self.view.backgroundColor = UIColor.black
        
        
        resetButton.frame = CGRect.init(x: self.view.frame.size.width-60, y: 40, width: 50, height: 20)
        showLable.frame = CGRect.init(x: 0, y: 60, width: self.view.frame.size.width, height: 140)
        startButton.frame = CGRect.init(x: 0, y: 200, width: self.view.frame.size.width * 0.5, height: self.view.frame.size.height - 200)
        stopButton.frame = CGRect.init(x: self.view.frame.size.width * 0.5, y: 200, width: self.view.frame.size.width * 0.5, height: self.view.frame.size.height - 200)
        
    }
    
    func setAction() {
        resetButton.addTarget(self, action: #selector(reset), for: .touchUpInside)
        startButton.addTarget(self, action: #selector(start), for: .touchUpInside)
        stopButton.addTarget(self, action: #selector(stop), for: .touchUpInside)
    }
    
    @objc func reset() {
        
        timer.invalidate()
        counter = 0.0
        isPaying = false
        showLable.text = String(counter)
    }
    
    @objc func start(){
    
        if isPaying == true {
            return
        }
        timer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true, block: { (t) in
            self.counter += 0.1
            self.showLable.text = String(self.counter)
        })
        isPaying = true
    }
    
    @objc func stop(){
        timer.invalidate()
        isPaying = false
    }
    
}

